import { ajax } from '../../js/ajax.js';

//#region 查询的部分

let txtName = document.getElementById('txtName');
let txtNickname = document.getElementById('txtNickname');
let txtSex = document.getElementById('txtSex');
let imgUser = document.getElementById('imgUser');

let divUser = document.getElementById('divUser');
let divInfo = document.getElementById('divInfo');
let divLoading = document.getElementById('divLoading');

divUser.style.display = 'none';
divInfo.style.display = 'none';

let tbUser;
let tbUserInfo;

// 获取登录用户的信息
function queryUserInfo() {
  ajax.post('/user/auth/getUserInfo', {}, (data) => {
    divLoading.style.display = 'none';
    if (data.success) {
      // 记录接口返回的登录的用户信息
      tbUser = data.tbUser;
      tbUserInfo = data.tbUserInfo;
      showUserInfo();
      divUser.style.display = 'block';
    } else {
      alert('用户没有登录');
      divInfo.style.display = 'block';
    }
  });
}
let sexInfo = {
  m: '男',
  f: '女',
  n: '保密',
};

// 显示用户信息
function showUserInfo() {
  txtName.innerHTML = tbUser.username;
  txtNickname.innerHTML = tbUser.nickname;
  txtSex.innerHTML = sexInfo[tbUserInfo.sex];

  if (tbUserInfo.img) {
    imgUser.setAttribute('src', tbUserInfo.img);
  } else {
    // 没有头像就给一个默认值
    imgUser.setAttribute('src', '../batman-logo-04.jpg');
  }
}

queryUserInfo();

//#endregion

//#region 修改信息的部分

// 显示要修改的信息
let btnShowModify = document.getElementById('btnShowModify');
let divModify = document.getElementById('divModify');
let txt_img = document.getElementById('txt_img');
let txt_info = document.getElementById('txt_info');
let txt_nickname = document.getElementById('txt_nickname');
let txt_qq = document.getElementById('txt_qq');
let sexm = document.getElementById('sexm');
let sexf = document.getElementById('sexf');
let sexn = document.getElementById('sexn');
let txt_wechat = document.getElementById('txt_wechat');
let btnSave = document.getElementById('btnSave');

btnShowModify.addEventListener('click', () => {
  // 原始信息的显示
  txt_img.value = tbUserInfo.img;
  txt_info.value = tbUserInfo.info;
  txt_nickname.value = tbUser.nickname;
  txt_qq.value = tbUserInfo.qq;
  txt_wechat.value = tbUserInfo.wechat;
  if (tbUserInfo.sex == 'm') {
    sexm.checked = true;
  } else if (tbUserInfo.sex == 'f') {
    sexf.checked = true;
  } else if (tbUserInfo.sex == 'n') {
    sexn.checked = true;
  }

  divModify.style.display = 'block';
});

// 保存修改信息
btnSave.addEventListener('click', () => {
  // 性别的处理
  let sex = '';
  if (sexm.checked) {
    sex = 'm';
  } else if (sexf.checked) {
    sex = 'f';
  } else if (sexn.checked) {
    sex = 'n';
  }
  // 修改的信息
  let info = {
    img: txt_img.value,
    info: txt_info.value,
    nickname: txt_nickname.value,
    qq: txt_qq.value,
    wechat: txt_wechat.value,
    sex: sex,
  };

  ajax.post('/user/auth/updateUserInfo', info, (data) => {
    alert(data.message);
    // 更新用户信息
    queryUserInfo();
  });
});

//#endregion

//#region 邮箱绑定的部分
let btnShowEMail = document.getElementById('btnShowEMail');
let divEMail = document.getElementById('divEMail');
let btnSendEmail = document.getElementById('btnSendEmail');
let txt_email = document.getElementById('txt_email');
let txt_code = document.getElementById('txt_code');
let btnEmail = document.getElementById('btnEmail');

btnShowEMail.addEventListener('click', () => {
  txt_email.value = tbUserInfo.email;
  divEMail.style.display = 'block';
});

btnSendEmail.addEventListener('click', () => {
  ajax.post(
    '/tool/sendEmailCode',
    {
      email: txt_email.value,
    },
    (data) => {
      alert(data.message);
    }
  );
});

btnEmail.addEventListener('click', () => {
  let info = {
    email: txt_email.value,
    code: txt_code.value,
  };

  ajax.post('/user/auth/updateUserEmail', info, (data) => {
    alert(data.message);
    // 更新用户信息
    queryUserInfo();
  });
});

//#endregion

//#region 安全退出
let btnLogout = document.getElementById('btnLogout');

btnLogout.addEventListener('click', () => {
  ajax.post('/user/auth/logout', {}, () => {
    alert('用户退出成功');
    location.reload();
  });
});

//#endregion
